import { isType } from './index'
/** 前端模拟分页
 *@params {array} source  数据
 *@params {number} page 第几页
 *@params {number} size 每页大小
 *@returns {object} 返回数据，总条数，第几页，页大小
 */
export default function pagination(source, page, size) {
  const result = {
    data: [],
    total: 0,
    page,
    size
  }
  if (!isType(source, 'array')) {
    return result
  }
  result.total = source.length
  const maxIndex = source.length - 1
  page--
  let offset = 0
  let startIndex = 0
  if (page > 0) {
    offset = page * size
    startIndex = offset
  }
  const endIndex = startIndex + size
  if (startIndex > maxIndex) {
    return result
  }
  if (startIndex <= maxIndex) {
    if (endIndex >= maxIndex) {
      result.data = source.slice(startIndex)
    } else if (endIndex < maxIndex) {
      result.data = source.slice(startIndex, endIndex)
    }
    return result
  }
}
